package web.servlet;

import domain.Account;
import domain.Product;
import domain.User;
import service.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class SignOnServlet extends HttpServlet {

    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";
    private String username;
    private String password;
    private String checkCode;
    private OrderService orderService;
    private String Msg = null;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        this.checkCode = req.getParameter("checkcode");
        orderService = new OrderService();
        HttpSession session = req.getSession();//进行会话跟踪
        String code = (String) session.getAttribute("code");
        //校验用户输入的正确性
        if(!Objects.equals(code, "") && checkCode.equals(code)){
            if(!validate()){
                req.setAttribute("signOnMsg",this.Msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
            }else {
                AccountService accountService = new AccountService();
                Account loginAccount = accountService.getAccount(username,password);
                if(loginAccount != null){ //用户名和密码正确，跳入主页面
                    String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                            + req.getContextPath() + req.getServletPath() + "?" + req.getQueryString();

                    LogService logService = new LogService();
                    String logInfo = logService.logInfo(" ") + strBackUrl + " 用户登录";
                    logService.insertLogInfo(loginAccount.getUsername(), logInfo);

                    session.setAttribute("username",loginAccount.getUsername());
                    session.setAttribute("loginAccount",loginAccount);
                    if(loginAccount.isListOption()){
                        CatalogService catalogService = new CatalogService();
                        List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                        session.setAttribute("myList",myList);
                    }

                    resp.sendRedirect("mainForm");//请求重定向，访问mainForm的Servlet，打开main.jsp
                }else { //用户名或密码不正确，重新登录
                    this.Msg = "用户名或密码不正确，请重新登录或注册新用户";
                    req.setAttribute("signOnMsg",this.Msg);
                    req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
                }
            }
        }else{
            this.Msg = "验证码不正确！请重新输入";
            req.setAttribute("signOnMsg",this.Msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req,resp);
        }

    }

    private boolean validate(){
        if(this.username == null || this.username.equals("")){
            this.Msg = "用户名不能为空";
            return false;
        }
        if(this.password == null || this.password.equals("")){
            this.Msg = "密码不能为空";
            return false;
        }
        return true;
    }
}
